<template>
  <div class="repair-order">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Report_for_repair_work") }}</label>
      </button>
    </div>

    <div>
      <div>
        <div>
          <el-select
            style="margin-left: 10px"
            @change="workshopChange"
            v-model="workshop"
            :placeholder="$t('route.Please_select_workshop')"
          >
            <el-option
              v-for="item in workshopList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div>
          {{ $t("route.Machine_repair_team") }}
        </div>
        <div class="div-handle-right">
          <el-button @click="jumpPage(0)">{{ $t("route.Add_Members") }}</el-button>
          <el-button @click="jumpPage(1)">{{
            $t("route.Publication_of_conventions")
          }}</el-button>
        </div>
      </div>
      <div>
        <el-scrollbar>
          <div class="div-mechanic">
            <div v-for="item in mechanicList">
              <img :src="item.avatar" />
              <label>{{ item.nickname }}</label>
              <label>{{ $t("route.post") }}：{{ $t("route.Mechanic") }}</label>
              <label
                >{{ $t("route.working_years") }}：{{ formatEntry(item.entry)
                }}{{ $t("route.day") }}</label
              >
              <label>{{ $t("route.score") }}：5分</label>
              <el-button @click="jumpPage(3, item.nickname)" :plain="true">{{
                $t("route.maintenance_record")
              }}</el-button>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>

    <div>
      <div>
        <el-select
          @change="kindChange"
          style="margin-left: 10px"
          v-model="query.kind"
          :placeholder="$t('route.Please_select_the_type')"
        >
          <el-option v-for="item in kindList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>

        <div class="div-handle-right">
          <el-button @click="batchDeleteClick">{{ $t("route.Delete") }}</el-button>
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Head_Model')" />
        </div>
      </div>

      <el-table
        @selection-change="selectionLineChangeHandle"
        class="table"
        :data="repairList"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column :label="$t('route.state')" width="100">
          <template #default="scope">
            <label :style="{ color: formatColor(scope.row.state) }">{{
              formatState(scope.row.state)
            }}</label>
          </template>
        </el-table-column>
        <el-table-column prop="kind" width="150" :label="$t('route.kind')" />
        <el-table-column prop="noseCode" width="130" :label="$t('route.Head_Model')" />
        <el-table-column :label="$t('route.location')" width="130">
          <template #default="scope">
            {{ scope.row.workshop ? scope.row.workshop : scope.row.store }}
          </template>
        </el-table-column>
        <el-table-column prop="problem" :label="$t('route.Maintenance_issues')" />
        <el-table-column
          prop="nickname"
          width="80"
          :label="$t('route.Repair_applicant')"
        />
        <el-table-column :label="$t('route.Repair_time')" width="170">
          <template #default="scope">
            {{ new Date(scope.row.createTime).format("yyyy-MM-dd hh:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column width="80" :label="$t('route.Processing_results')">
          <template #default="scope">
            <label
              :style="{
                color:
                  formatResult(scope.row.result) == $t('route.normal')
                    ? '#444444'
                    : '#FF0000',
              }"
              >{{ formatResult(scope.row.result) }}</label
            >
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.operate')" width="250">
          <template #default="scope">
            <el-button type="text" @click="jumpPage(2, scope.row)">{{
              $t("route.search")
            }}</el-button>
            <el-button type="text" @click="jumpPage(4, scope.row.noseCode)">{{
              $t("route.Details")
            }}</el-button>
            <el-button
              v-if="scope.row.state != 3 && scope.row.state != 0"
              type="text"
              @click="capitalRepairClick(scope.row)"
              >{{ $t("route.Overhaul") }}</el-button
            >
            <el-button
              @click="solveClick(scope.row.id)"
              v-if="scope.row.state != 3 && scope.row.state != 0"
              style="color: #009933"
              type="text"
              >{{ $t("route.solve") }}
            </el-button>
            <el-button
              style="color: #ff0000"
              @click="deleteClick(scope.row.id)"
              type="text"
              >{{ $t("route.Delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :title="$t('route.Overhaul_equipment_addition')"
      width="30%"
      v-model="dialogShow1"
      draggable
    >
      <el-form ref="form" label-width="150px">
        <el-form-item :label="$t('route.Overhaul_problems')">
          <el-input class="input" v-model="form.problem" />
        </el-form-item>
        <el-form-item :label="$t('route.Whether_to_purchase_new_parts')">
          <el-switch v-model="form.needBuy" />
        </el-form-item>
        <el-form-item :label="$t('route.Whether_there_are_damaged_parts')">
          <el-switch v-model="form.hasPart" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveClick">{{ $t("route.sure") }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from "../../js/common/constant.js";
import DateUtils from "../../js/common/date-utils.js";
import i18n from "../../i18n/index";
export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
        kind: null,
      },
      workshop: i18n.global.t("route.All_workshops"),
      repairList: [],
      kindList: [i18n.global.t("route.All_categories"), ...Constant.DEVICE_KIND],
      mechanicList: [],
      checkRows: [],
      dialogShow1: false,
      selRepair: null,
      form: {
        needBuy: false,
        hasPart: false,
        problem: null,
      },
    };
  },
  methods: {
    saveClick() {
      if (CommonUtils.isEmpty(this.form.problem)) {
        this.$message.error(i18n.global.t("route.Overhaul_problem_cannot_be_empty"));
        return;
      }
      http.post(
        api.CAPITAL_REPAIR,
        {
          repairId: this.selRepair.id,
          problem: this.form.problem,
          needBuy: this.form.needBuy,
          hasPart: this.form.hasPart,
        },
        (data) => {
          this.$message.success(i18n.global.t("route.Saved_successfully"));
          this.getRepairList();
        }
      );
      this.dialogShow1 = false;
    },

    selectionLineChangeHandle(val) {
      this.checkRows = val;
    },
    batchDeleteClick() {
      if (this.checkRows.length == 0) return;
      this.$confirm(i18n.global.t("route.continue14"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        var repairIdList = [];
        for (var item of this.checkRows) {
          repairIdList.push(item.id);
        }
        http.post(
          api.DELETE_REPAIR_LIST,
          {
            repairIdList: repairIdList,
          },
          (data) => {
            this.$message.success(i18n.global.t("route.Delete_succeeded"));
            this.getRepairList();
          }
        );
      });
    },
    deleteClick(repairId) {
      this.$confirm(i18n.global.t("route.continue15"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        http.post(
          api.DELETE_REPAIR,
          {
            repairId: repairId,
          },
          (data) => {
            this.$message.success(i18n.global.t("route.Delete_succeeded"));
            this.getRepairList();
          }
        );
      });
    },
    formatState(state) {
      if (state == 0) {
        return i18n.global.t("route.Delete_succeeded");
      } else if (state == 1) {
        return i18n.global.t("route.Not_processed");
      } else if (state == 2) {
        return i18n.global.t("route.Processing");
      } else if (state == 3) {
        return i18n.global.t("route.Expired");
      }
    },
    formatColor(state) {
      if (state == 0) {
        return "#444444";
      } else if (state == 1) {
        return "#FF0000";
      } else if (state == 2) {
        return "#00FF00";
      } else if (state == 3) {
        return "#888888";
      }
    },
    formatResult(result) {
      //0:没有异常 1:普通维修 2:大修 3报废 4:异常报警
      if (result == 0) {
        return i18n.global.t("route.normal");
      } else if (result == 1) {
        return i18n.global.t("route.General_maintenance");
      } else if (result == 2) {
        return i18n.global.t("route.Overhaul");
      } else if (result == 3) {
        return i18n.global.t("route.Scrap");
      } else if (result == 4) {
        return i18n.global.t("route.Abnormal_alarm");
      }
    },
    workshopChange() {
      this.getAllMechanic();
    },

    searchClick() {
      this.query.pageNum = 1;
      this.getRepairList();
    },
    solveClick(repairId) {
      http.post(
        api.UPDATE_REPAIR,
        {
          repairId: repairId,
          state: 0,
          result: 0,
        },
        (data) => {
          this.$message.success(i18n.global.t("route.Saved_successfully"));
          this.getRepairList();
        }
      );
    },
    capitalRepairClick(repair) {
      this.selRepair = repair;
      this.dialogShow1 = true;
    },
    formatEntry(entry) {
      if (!entry) return "---";
      return DateUtils.getDateDif(entry, new Date().format("yyyy-MM-dd"));
    },
    kindChange() {
      this.query.pageNum = 1;
      this.getRepairList();
    },

    getRepairList() {
      http.post(
        api.GET_REPAIR_LIST8,
        {
          adminId: this.$store.state.user.id,
          search: this.query.search,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          kind:
            this.query.kind == i18n.global.t("route.All_categories")
              ? null
              : this.query.kind,
        },
        (data) => {
          this.repairList = data.data.repairList;
          this.query.total = data.data.total;
        }
      );
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getRepairList();
    },
    jumpPage(type, param1) {
      if (type == 0) {
        this.$router.push({
          name: "add-user",
          params: {
            userType: 16,
          },
        });
      } else if (type == 1) {
        this.$router.push("/add-notice");
      } else if (type == 2) {
        this.$router.push({
          name: "repair-info",
          params: {
            repair: JSON.stringify(param1),
          },
        });
      } else if (type == 3) {
        this.$router.push({
          name: "machine-repair",
          params: {
            search: param1,
          },
        });
      } else if (type == 4) {
        this.$router.push({
          name: "repair-record",
          params: {
            noseCode: param1.noseCode,
          },
        });
      }
    },

    getAllWorkshop() {
      http.post(
        api.GET_ALL_WORKSHOP,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          this.workshopList = data.data;
          this.workshopList.splice(0, 0, {
            id: null,
            name: i18n.global.t("route.All_workshops"),
            code: i18n.global.t("route.All_workshops"),
          });
        }
      );
    },
    getAllMechanic() {
      http.post(
        api.GET_ALL_USER1,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          userType: 16,
          workshop:
            this.workshop == i18n.global.t("route.All_workshops") ? null : this.workshop,
        },
        (data) => {
          this.mechanicList = data.data;
        }
      );
    },
    getAllKind() {
      http.post(
        api.GET_ALL_KIND,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.kindList = [i18n.global.t("route.All_categories"), ...data.data];
        }
      );
    },
  },
  created() {
    this.getAllKind();
    this.getAllWorkshop();
    this.getRepairList();
    this.getAllMechanic();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.repair-order {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px 0px 20px 20px;
    border-radius: 5px;
    display: flex;
    flex-direction: column;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;

      > div {
        flex: 1;
      }

      > div:nth-of-type(2) {
        color: #000000;
        flex: 1;
        text-align: center;
        font-weight: 800;
        font-size: 25px;
      }

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > button {
          color: #a866ff;
          margin-right: 10px;
          padding: 4px 20px;
        }
      }
    }

    .div-mechanic {
      display: flex;
      flex-direction: row;
      margin-top: 5px;
      border-top: 1px solid #eeeeee;
      width: 1200px;

      > div {
        background-color: #ffffff;
        padding: 10px;
        margin: 10px 10px 0px 10px;
        border: 2px solid #eeeeee;
        border-radius: 5px;
        display: flex;
        flex-direction: column;
        align-items: center;

        > img:nth-of-type(1) {
          width: 80px;
          height: 80px;
          border-radius: 5px;
        }

        > label:nth-of-type(1) {
          margin-top: 5px;
          font-size: 16px;
          color: #000000;
        }

        > label:nth-of-type(2),
        > label:nth-of-type(3),
        > label:nth-of-type(4) {
          width: 150px;
          text-align: center;
          margin-top: 2px;
          font-size: 13px;
          color: #333333;
        }

        > button:nth-of-type(1) {
          margin-top: 5px;
          color: #a866ff;
          padding: 2px 20px;
        }
      }
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }
}
</style>
